package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.fragment.NumberPickerDialogFragment.NumberClickListener;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 添加加班类型
 * 
 * @author li.li
 *
 */
public class HolidayTypeAddFragment extends BaseProgressFragment implements OnCheckedChangeListener {

	@Bind(R.id.id_holiday_leave_name)
	protected TextView name;

	@Bind(R.id.id_holiday_leave_color)
	protected RadioGroup colorTab;

	@Bind(R.id.id_holiday_leave_duration)
	protected TextView durationText;

	@Bind(R.id.id_holiday_leave_remark)
	protected TextView remark;

	private int duration;

	//当前选择的颜色 
	private String checkColor;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_holiday_type_add;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {
		colorTab.setOnCheckedChangeListener(this);
	}

	@Override
	protected void initData() {

	}

	@OnClick(R.id.id_holiday_leave_duration)
	public void clickDuration() {

		NumberPickerDialogFragment dialogFrag = new NumberPickerDialogFragment(new NumberClickListener() {

			@Override
			public void onEnsure(int number) {

				if (number > 0) {
					durationText.setText(number + "天");
					HolidayTypeAddFragment.this.duration = number;
				}
			}
		}, 1, 150, "天");

		dialogFrag.show(getChildFragmentManager(), dialogFrag.getClass().getSimpleName());

	}

	@Override
	public void onCheckedChanged(RadioGroup button, int checkedId) {

		switch (checkedId) {
		case R.id.id_home_item_arrange_type_color_1:

			checkColor = getString(R.color.cl_home_item_arrange_type_color_1);

			break;

		case R.id.id_home_item_arrange_type_color_2:

			checkColor = getString(R.color.cl_home_item_arrange_type_color_2);

			break;

		case R.id.id_home_item_arrange_type_color_3:

			checkColor = getString(R.color.cl_home_item_arrange_type_color_3);

			break;

		case R.id.id_home_item_arrange_type_color_4:

			checkColor = getString(R.color.cl_home_item_arrange_type_color_4);

			break;

		default:
			break;
		}

	}

	@OnClick(R.id.id_account_submit_btn)
	public void submit() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			if (StringUtils.isBlank(name.getText().toString())) {
				ViewUtils.toast("请输入休假名称");
				return;
			}

			if (StringUtils.isBlank(checkColor)) {
				ViewUtils.toast("请选择图标颜色");
				return;
			}

			if (duration == 0) {
				ViewUtils.toast("请选择休假时长");
				return;
			}

			RequestParams params = new RequestParams();

			params.put("uid", tokenKeeper.getUser().getUid());
			params.put("name", name.getText().toString());
			params.put("start", 0);
			params.put("end", 0);
			params.put("type", 3);//休假
			params.put("hour", duration * 8);
			params.put("color", checkColor);

			if (StringUtils.isNotBlank(remark.getText().toString())) {
				params.put("remark", remark.getText().toString());
			}

			ApiRequest.arrangeTypeInfoAdd.request(this, params);

		}

	}

	public static void go(Activity act) {

		Bundle args = new Bundle();
		MyPageHelper.holidayLeaveTypeAddFragment.showMyPage(act, args);

	}

}
